package edu.spec.wechat.kit;

/**
 * Created by MadFrog on 2016/10/30.
 */

import edu.spec.pms.model.enumeration.BindStatus;
import edu.spec.pms.model.student.Student;
import edu.spec.pms.service.iface.IStudentService;
import edu.spec.pms.service.iface.IUserService;
import edu.spec.wechat.service.iface.ISignInService;
import edu.spec.wechat.service.iface.ISignInSumService;
import edu.spec.wechat.web.BeanFactoryContext;
import org.madgecko.basic.kit.BasicKit;

import java.util.Date;
import java.util.Map;

/**
 * 事件消息处理工具
 */
public class EventMessageKit {
    /**
     *  事件消息包括以下：
     * 1 关注/取消关注事件
     * 2 扫描带参数二维码事件
     * 3 上报地理位置事件
     * 4 自定义菜单事件
     * 5 点击菜单拉取消息时的事件推送
     * 6 点击菜单跳转链接时的事件推送
     */

    /**
     * 处理事件消息
     * @return
     */
    public static String handleMessage(Map<String, Object> map){
        String replayContent = "";
        //消息是“上报地理位置”
        String event = (String)map.get("Event");
        if (BasicKit.isEmpty(event)){
            return replayContent;
        }
        if ("LOCATION".equals(event)){
            replayContent = handleLocationEvent(map);
        }else if("subscribe".equals(event)){
            replayContent = handleSubscribe(map);
        }else if ("unsubscribe".equals(event)){
            replayContent = handleUnsubscribe(map);
        }
        return replayContent;
    }

    /**
     * 处理用户"关注"微信公众号事件
     * @return
     */
    private static String handleSubscribe(Map<String, Object> map){
        //获取openId
        //检查该openId是否绑定学号
        //      未绑定：提醒用户立即绑定学号，一经绑定不能修改，使用链接跳转
        //      已绑定：提醒用户该微信号之前已经绑定过某某学号，原来数据还在
        String replay = null;
        String bindingUrl = "https://www.baidu.com/";
        String openId = (String)map.get("FromUserName");
        IStudentService studentService = (IStudentService)BeanFactoryContext.getService("studentService");
        Student student = studentService.loadByOpenId(openId);
        if (BindStatus.BINDED.equals(student.getBindStatus())){
            replay = "欢迎再次使用，您之前已经绑定过学号，请直接使用。";
        }else {
            replay = "<a href=\"" + bindingUrl + "\">请立即完成学号绑定学号。</a>";
        }
        return replay;
    }

    /**
     * 处理用户"取消关注"微信公众号事件
     * @return
     */
    private static String handleUnsubscribe(Map<String, Object> map){
        String replay = null;
        String bindingUrl = "https://www.baidu.com/";
        String openId = (String)map.get("FromUserName");
        IStudentService studentService = (IStudentService)BeanFactoryContext.getService("studentService");
        Student student = studentService.loadByOpenId(openId);
        if(!BasicKit.isEmpty(student)){
//            student.setBindStatus(BindStatus.UNBIND);
//            studentService.update(student);
        }
        return replay;
    }

    /**
     * 处理用户"上报地理位置事件"
     * @param map
     * @return
     */
    private static String handleLocationEvent(Map<String, Object> map){
        String replayContent = "";
        String openId = (String)map.get("openId");
        //学生签到
        //检查是不是重复签到，通过最后签到日期来判断
        ISignInSumService signInSumService = (ISignInSumService)BeanFactoryContext.getService("signInSumService");
        Date lastSignInDate = signInSumService.loadByOpenId(openId).getLastSignInDate();
        Date currentDate = new Date();
        if(currentDate.equals(lastSignInDate)){
            replayContent =  "您今日已经签到，每日签到一次即可，请勿重复签到。";
        }
        //保存签到记录SignIn
        ISignInService signInService = (ISignInService)BeanFactoryContext.getService("signInService");
        signInService.signIn(map);
        //TODO 更新SignInSum
        replayContent  =  "今日成功签到，明天继续努力哦。";
        return replayContent;
    }

}
